package com.javaweb.blogcite.controller;


import com.javaweb.blogcite.aspect.JwtCheck;
import com.javaweb.blogcite.common.ResponseResult;
import com.javaweb.blogcite.common.PageData;
import com.javaweb.blogcite.pojo.Message;
import com.javaweb.blogcite.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class MessageController {
    @Resource
    private MessageService messageService;

    @PostMapping("/api/message")
    public ResponseResult<Message> postMessage(@RequestBody Message message) {
        Message mes = messageService.postMessage(message);
        ResponseResult<Message> result = new ResponseResult<>();
        if (mes != null) {
            result.setCode(0);
            result.setMsg("success");
            result.setData(mes);
        } else {
            result.setCode(500);
            result.setMsg("插入数据失败！");
        }
        return result;
    }

    @JwtCheck
    @DeleteMapping("/api/message/{id}")
    public ResponseResult deleteMessage(@PathVariable("id") Integer id) {
        boolean success = messageService.deleteMessage(id);
        ResponseResult result = new ResponseResult();
        if (success) {
            result.setCode(0);
            result.setMsg("success");
        } else {
            result.setCode(500);
            result.setMsg("删除数据失败！");
        }
        return result;
    }

    @GetMapping("/api/message")
    public ResponseResult<PageData<Message>> getMessagesByPage(Integer page, Integer limit) {
        PageData<Message> pageData = messageService.getMessagesByPage(page, limit);
        ResponseResult<PageData<Message>> result = new ResponseResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(pageData);
        return result;
    }
}
